package com.unohacha.qqj.fzdr.personal.view.activity;

import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.unohacha.qqj.fzdr.R;
import com.unohacha.qqj.fzdr.common.bean.HttpResult;
import com.unohacha.qqj.fzdr.common.http.RetrofitManager;
import com.unohacha.qqj.fzdr.common.utils.StringUtil;
import com.unohacha.qqj.fzdr.common.utils.TemporaryDataUtil;
import com.unohacha.qqj.fzdr.my.MyInfoCenterActivity;
import com.unohacha.qqj.fzdr.personal.presenter.ChangePasswordPresenter;
import com.unohacha.qqj.fzdr.personal.view.IChangePasswordView;
import com.unohacha.qqj.fzdr.util.Prefer;
import com.unohacha.qqj.mylibrary.base.BaseActivity;

import butterknife.BindView;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * 修改绑定手机号
 */
public class ChangePhoneActivity extends BaseActivity implements IChangePasswordView {

    private ChangePasswordPresenter changePasswordPresenter;

    @BindView(R.id.common_back)
    ImageView commonBack;
    @BindView(R.id.common_title)
    TextView commonTitle;
    @BindView(R.id.common_right_image)
    ImageView commonRightImage;
    @BindView(R.id.common_right_text)
    TextView commonRightText;

    @OnClick(R.id.common_back)
    public void setCommonBack() {
        finish();
    }

    @BindView(R.id.et_mobile)
    EditText etMobile;

    @BindView(R.id.et_verify)
    EditText etVerify;


    @OnClick(R.id.tv_send_verification)
    void onSendVerification() {

        // 读取输入手机号码
        String mobile = etMobile.getText().toString().trim();
        // 正则 验证手机号的可行性
        if (StringUtil.isMobile(mobile)) {
            // 发送验证码 修改手机号
            changePasswordPresenter.sendVerifyCode("4", mobile);
        } else {
            Toast.makeText(getApplicationContext(), "请输入合法的手机号", Toast.LENGTH_SHORT).show();
        }
    }

    // 完成 修改绑定手机
    @OnClick(R.id.btn_change_complete)
    void onChangeCompleteClick() {
        Call<HttpResult<String>> call = RetrofitManager.getInstance().revisetele(
                Prefer.getInstance().getToken(), TemporaryDataUtil.getUserInfoBean().getTelephone(),
                etVerify.getText().toString().trim());
        call.enqueue(new Callback<HttpResult<String>>() {
            @Override
            public void onResponse(Call<HttpResult<String>> call, Response<HttpResult<String>> response) {
                String data = response.body().getData();
                String msg = response.body().getMessage();
                if (data != null) {
                    // TODO
                }
            }

            @Override
            public void onFailure(Call<HttpResult<String>> call, Throwable t) {

            }
        });

        changePasswordPresenter.changeMobile("", "");
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_change_phone;
    }

    @Override
    protected void initView() {
//        super.initView();
        commonTitle.setVisibility(View.INVISIBLE);
        commonRightImage.setVisibility(View.INVISIBLE);
        commonRightText.setVisibility(View.INVISIBLE);

        changePasswordPresenter = new ChangePasswordPresenter(this);
    }


    // 验证码发送结果
    @Override
    public void sendVerifyCode(String data) {
        Toast.makeText(getApplicationContext(), "验证码：" + data, Toast.LENGTH_SHORT).show();
    }

    // 修改密码成功
    @Override
    public void ChangeSuccess() {
        Toast.makeText(getApplicationContext(), "确定修改", Toast.LENGTH_SHORT).show();
        jumpToOtherActivity(this, MyInfoCenterActivity.class);
        finish();
    }

    // 修改密码失败
    @Override
    public void changeFailed() {
        Toast.makeText(getApplicationContext(), "确定修改密码", Toast.LENGTH_SHORT).show();
    }
}
